layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;
	var upload = layui.upload;

	var table_filter = 'brandcert_list',
		form_filter = 'brandcert_search_form';
	var where_json = {
		_o: 'id',
		_b: 'desc',
		chk_supplier:chk_supplier
	};
	// 渲染表格
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: brandcert_list_url,
		method: 'post',
		height: 'full-200',
		where: {
			_o: 'id',
			_b: 'desc',
			chk_supplier:chk_supplier
		},
		cols: [brandcert_cols],
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 30,
			theme: '#393d49',
			groups: 8
		},
		autoSort: false,
		toolbar: '#brandcertToolbar',
		defaultToolbar: [],
		initSort: {
			field: 'id',
			type: 'desc'
		},

	});

	/* table.on('edit(baojia_list)',function(obj){
		console.log(obj.value)
	}) */



	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		switch (obj.event) {
			case 'addBrandcert':
				create_tab($(this));
				break;
			default:
				break;
		}
		return false;
	})
	form.render();
	var select_search_list = function(field, value) {
		where_json[field] = value;
	};
	form.on('select', function(data) {
		var value = data.value;
		var e = data.elem;
		var id = $(e).attr('id');
		select_search_list(id, value);
	});
	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		select_search_list(name, value);
	});

	// 申请时间搜索
	var min_create_time = laydate.render({
		elem: 'input[name="min_create_time"]',
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search_list('min_create_time', value);
		}
	});
	var max_create_time = laydate.render({
		elem: 'input[name="max_create_time"]',
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search_list('max_create_time', value);
		}
	});
	// 审核时间
	var min_audit_time = laydate.render({
		elem: 'input[name="min_audit_time"]',
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search_list('min_audit_time', value);
		}
	});
	var max_audit_time = laydate.render({
		elem: 'input[name="max_audit_time"]',
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search_list('max_audit_time', value);
		}
	});
	// 生效时间
	var min_start_date = laydate.render({
		elem: 'input[name="min_start_date"]',
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search_list('min_start_date', value);
		}
	});
	var max_start_date = laydate.render({
		elem: 'input[name="max_start_date"]',
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search_list('max_start_date', value);
		}
	});
	// 截止时间
	var min_end_date = laydate.render({
		elem: 'input[name="min_end_date"]',
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search_list('min_end_date', value);
		}
	});
	var max_end_date = laydate.render({
		elem: 'input[name="max_end_date"]',
		theme: '#393D49',
		done: function(value, date, endDate) {
			select_search_list('max_end_date', value);
		}
	});
	// 时间结束

	$('#brandcert_search_btn').on('click', () => {
		layer.load();
		table.reload(table_filter, {
			where: where_json,
			page: {
				curr: 1
			},
			done: function() {
				layer.closeAll();
			}
		});
	})

	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var baojia_id = data.baojia_id;
		switch (e) {
			case 'setOpen':
				send_info(brandcert_setOpen_url,{id:data.id},(data)=>{
					layer.msg('操作成功', {
						icon: 1,
						anim: 6
					});
					table.reload(table_filter);
				})
			break;
			case 'pass':
				layer.confirm("确认审核通过吗?", {
					icon: 3,
					title: '提示'
				}, function(index) {
					send_info(baojia_pass_url, {
						baojia_id: baojia_id
					}, function(data) {
						layer.closeAll();
						layer.msg('操作成功', {
							icon: 1,
							anim: 6
						});
						table.reload(table_filter);
					})
				})
				break;
			case 'refuse':
				layer.prompt({
					title: '拒绝原因'
				}, function(value, index, elem) {
					if (!value) {
						layer.msg('请输入拒绝原因', {
							icon: 2,
							anim: 6
						});
						return false;
					}
					send_info(baojia_refuse_url, {
						baojia_id: baojia_id,
						remark: value
					}, function(data) {
						layer.closeAll();
						layer.msg('操作成功', {
							icon: 1,
							anim: 6
						});
						table.reload(table_filter);
					})
				});
				break;
			case 'edit':
				create_tab($(this));
				break;
			default:
				break;
		}
	})
})